package com.xiumiing.net.base.uitls;

import android.Manifest;
import android.os.Environment;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;

import java.io.File;

/**
 * @desc   本地文件管理系统
 * @author guguofeng
 * @date   2019/3/13
 * @Copyright: 武汉市追忆那年网络科技有限公司
 */

public class FileManager {

    /**
     * CACHE_DIR : 其它文件的缓存
     * IMG_DIR : 图片
     * LOG_DIR : 日志
     * CRASH_DIR : 崩溃文件
     * FILE_DIR : 文件
     * BLOCKCANARY_DIR : 卡顿
     * H5_DIR : H5
     * TEMP_DIR : 临时缓存
     * CAMERA_DIR : 相册目录
     */
    private final static String CACHE_DIR = File.separator + "cache" + File.separator;
    private final static String IMG_DIR = File.separator + "picture" + File.separator;
    private final static String LOG_DIR = File.separator + "logi" + File.separator;
    private final static String CRASH_DIR = File.separator + "crash" + File.separator;
    private final static String FILE_DIR = File.separator + "file" + File.separator;
    private final static String BACK_UP_DIR = File.separator + "backup" + File.separator;
    private final static String DB_DIR = File.separator + "db" + File.separator;
    private final static String BLOCK_DIR = File.separator + "blockcanary" + File.separator;
    private final static String CAMERA_DIR = Environment.getExternalStorageDirectory()
            + File.separator + Environment.DIRECTORY_DCIM
            + File.separator + "Camera" + File.separator;;

    private final static String DATA_FILES = Utils.getApp().getFilesDir().getAbsolutePath() + File.separator + "zynn" + File.separator;
    private final static String H5_DIR = DATA_FILES + "h5" + File.separator;
    private final static String TEMP_DIR = DATA_FILES + "temp" + File.separator;


    private static FileManager instance;
    private String mRootPath;


    public enum DIRType {
        APP_FILES(DATA_FILES),
        /**
         * CACHE 目录
         */
        CACHE(CACHE_DIR),
        /**
         * 数据库
         */
        DB(DB_DIR),
        /**
         * 图片目录
         */
        IMAGE(IMG_DIR),
        /**
         * XLOG 目录
         */
        LOG(LOG_DIR),
        /**
         * 闪退日志目录
         */
        CRASH(CRASH_DIR),
        /**
         * 文件目录
         */
        FILE(FILE_DIR),
        /**
         * 文件备份目录
         */
        BACK_UP(BACK_UP_DIR),
        /**
         * H5存放目录
         */
        H5(H5_DIR),
        /**
         * 剪切图目录
         */
        TEMP(TEMP_DIR),

        /**
         * 卡顿
         */
        BLOCKCANARY(BLOCK_DIR),

        /**
         * 相册
         */
        CAMERA(CAMERA_DIR);

        private String path;

        DIRType(String path) {
            this.path = path;
        }

        public String getPath() {
            return path;
        }
    }

//    private FileManager() {
//        if (RxPermissionsUtils.isPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                && RxPermissionsUtils.isPermission(Manifest.permission.READ_EXTERNAL_STORAGE)){
//            mRootPath = getSdCardRootPath();
//        } else {
//            mRootPath = DATA_FILES;
//        }
//        LogUtils.d("mRootPath", "mRootPath--->" + mRootPath);
//        createDir(mRootPath);
//    }
//
//    public String getRootPath() {
//        return mRootPath;
//    }
//
//    public String getAppFilesPath() {
//        return App.getContext().getFilesDir().getAbsolutePath() + File.separator + "zynn" + File.separator;
//    }

    public String getSdCardRootPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "zynn" + File.separator;
    }

    public static FileManager getInstance() {
        if (null == instance) {
            synchronized (FileManager.class) {
                instance = new FileManager();
            }
        }
        return instance;
    }

    public String getFilePath(DIRType dirType, String fileName) {
        if (dirType.equals(DIRType.H5)) {
            createDir(dirType.getPath());
            return dirType.getPath() + fileName;
        } else {
            createDir(mRootPath + dirType.getPath());
        }
        return mRootPath + dirType.getPath() + fileName;
    }

    public File getFile(DIRType dirType, String fileName) {
        return new File(getFilePath(dirType, fileName));
    }

    public String getDir(DIRType dirType) {
        String path;
        if (dirType.equals(DIRType.H5)) {
            createDir(dirType.getPath());
            path = dirType.getPath();
        } else {
            createDir(mRootPath + dirType.getPath());
            path = mRootPath + dirType.getPath();
        }
        return path;
    }

    private void createDir(String filePath) {
        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }


}
